package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.Coin
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IOCBView
import de.baumann.browser.model.UserModel
import de.baumann.browser.model.coinList
import de.baumann.browser.utils.*

class OCBPresenter : BasePresenter<IOCBView>() {
    fun getList() {
        coinList()
                .subscribe(object : OdinObserver<List<Coin>>(view) {
                    override fun onSuccess(data: List<Coin>?) {
                        if (data != null)
                            view?.setCoinList(data)
                    }
                })
    }

    private val userModel = UserModel()
    fun login() {
        userModel.login(getMoneyAddress(), "")
                .subscribe(object : OdinObserver<LoginInfo>(view) {
                    override fun onSuccess(loginInfo: LoginInfo) {
                        saveUser(loginInfo)
                        UnicornUtil.setUserInfo(loginInfo)
                        saveInviteCode(loginInfo.inviteCode)
                        saveInviteCodeStatus(loginInfo.inviteId)
                        saveFormAddress(loginInfo.fromAddress)
                        saveLogin(true)
                    }
                })
    }
}